package com.example.shushiyunsst.fragment.main.wallet.sub

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.example.library_common.album.ui.AlbumPreviewFragment
import com.example.library_common.base.LazyFragment
import com.example.library_common.utils.even.MessageWrap
import com.example.library_common.utils.even.TO_REFRESH_BALANCE_INFO
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.getUserBalanceInfo
import com.example.shushiyunsst.scan.ScanFrameFragment
import com.example.shushiyunsst.scan.ScanResListener
import com.example.wallet.bean.BalanceInfoBean
import com.example.wallet.db.BalanceInfoManager
import com.example.wallet.global.AppGlobals
import com.senon.modularapp.util.JssSpannableString
import java.math.BigDecimal

class WithdrawFragment : LazyFragment() {

    companion object {
        fun newInstance(): WithdrawFragment {
            val args = Bundle()

            val fragment = WithdrawFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var feedingTv: TextView

    private lateinit var addressEdt: EditText

    private var moneyType = AppGlobals.getApplication().getString(R.string.wallet_sst)

    override fun getLayoutId() = R.layout.withdraw_layout_fragment

    override fun lazyInit() {
        val balanceInfoBean = BalanceInfoManager.mBalanceInfoManager.getBalanceInfoBean()
        if (balanceInfoBean == null) {
            getUserBalanceInfo(_mActivity)
            return
        }
        presentBalanceInfo(balanceInfoBean)
    }

    override fun initView(view: View) {

        feedingTv = view.findViewById(R.id.feedingTv)

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)

        mToolbar.title = "提现"

        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        addressEdt = view.findViewById(R.id.addressEdt)

//        loadRootFragment(R.id.wallet_header, WalletHeadFragment.newInstance())

        view.findViewById<View>(R.id.mSaner)
            .setOnClickListener {
                val newInstance = ScanFrameFragment.newInstance()
                newInstance.setScanResListener(object : ScanResListener {
                    override fun onResStr(resStr: String) {
                        addressEdt.setText(resStr)
                    }
                })
                start(newInstance)
            }

        view.findViewById<View>(R.id.forAll)


    }

    override fun handMessage(msg: MessageWrap?) {
        if (msg?.message == TO_REFRESH_BALANCE_INFO) {
            val balanceInfoBean = BalanceInfoManager.mBalanceInfoManager.getBalanceInfoBean()
            presentBalanceInfo(balanceInfoBean)
        }

    }

    private fun presentBalanceInfo(balanceInfoBean: BalanceInfoBean?) {
        val feeBeanBean = BalanceInfoManager.mBalanceInfoManager.getFeeBeanBean()
        val minerFees = feeBeanBean?.sst
        val availableValue = balanceInfoBean?.totalBalance?.toString()
        val spn = JssSpannableString(
            _mActivity,
            "可用${availableValue}$moneyType ,矿工费: $minerFees$moneyType"
        )
            .first(availableValue.toString()).textColor(R.color.PrimaryColor)
            .first(minerFees.toString()).textColor(R.color.PrimaryColor)


        feedingTv.text = spn
    }
}